<?php
namespace app\admin\controller;
use app\admin\model\capiModel;
use app\admin\model\orderModel;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
class Admincapital extends BasicAdmin{

    public $table = 'capital';
    public $capital_water = 'capital_water';
    public $user = 'user';

    /**
     * @return mixed
     * 平台
     */
    public function index(){
        $info = Db::name($this->table)->where(['id'=>1])->find();
        list($get, $db) = [$this->request->get(), Db::name($this->capital_water)];

        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $start = strtotime($start);
            $end = strtotime($end);
            $db->whereBetween('addtime', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }

        if(isset($get['type'])&&$get['type']!==''){
            $db->where("type",$get['type']);
        }
        $where['cid'] = $info['id'];
        $list = parent::_list($db->where($where)->order('addtime DESC'),1,'','');
        $capiMoodel = new capiModel();
        $list['list'] = $capiMoodel::getTypeName($list['list'],1);
        $list['list'] = parent::dateYmd($list['list']);
        $list['type'] = $capiMoodel::cApiType(1);
        $orderModel = new orderModel();
        $list['list'] = $orderModel->getSn($list['list']);
        //资金统计

        $list['allmoney'] = 0;
        $list['newmoney'] = 0;

        if(!empty($list['list'])){
            //当前页
            foreach ($list['list'] as $value){
                $list['newmoney']  += $value['money'];
            }
            //全部
            $alldb= Db::name($this->capital_water);
            $allwhere['cid'] = $info['id'];
            if(isset($get['type'])&&$get['type']!==''){
                $allwhere['type'] = $get['type'];
            }
            $list['allmoney'] = $alldb->where($allwhere)->sum('money');
        }

        return $this->fetch('', ['title' => '平台资金明细','list'=>$list]);
    }

    /**
     * 用户
     */
    public function user(){

        list($get, $db) = [$this->request->get(), Db::name($this->capital_water)->alias('a')->join('t_capital b','a.cid=b.id','LEFT')->join('t_user c','a.uid=c.id','LEFT')->field('a.cid,a.type,a.uid,a.order_id,a.money,a.addtime,a.b_username,a.name,c.username,c.nickname')];
        //时间
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $start = strtotime($start);
            $end = strtotime($end);
            $db->whereBetween('a.addtime', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        if(isset($get['type'])&&$get['type']!==''){
            $db->where("a.type",$get['type']);
        }

        //账号
        if(isset($get['username'])&&$get['username']!==''){
            $db->where("c.username",trim($get['username']));
        }
        $where = 'b.type = 2 AND a.uid <> 1';
        $list = parent::_list($db->where($where),1,'','');
        $capiMoodel = new capiModel();
        $list['type'] = $capiMoodel::cApiType(2);
        $list['list'] = $capiMoodel::getTypeName($list['list'],2);
        $list['list'] = parent::dateYmd($list['list']);
        $orderModel = new orderModel();
        $list['list'] = $orderModel->getSn($list['list']);
        //资金统计
        $list['allmoney'] = 0;
        $list['newmoney'] = 0;
        if(!empty($list['list'])){
            //当前页
            foreach ($list['list'] as $value){
                $list['newmoney']  += $value['money'];
            }
            //全部
            $allwhere =  'b.type = 2 AND a.uid <> 1';

            if(isset($get['type'])&&$get['type']!==''){
                $allwhere .= ' AND a.type='.$get['type'];
            }
            //账号
            if(isset($get['username'])&&$get['username']!==''){
                $allwhere .= ' AND c.username ='.trim($get['username']);
            }

            $list['allmoney'] = Db::name($this->capital_water)->alias('a')
                ->join('t_capital b','a.cid=b.id','LEFT')
                ->join('t_user c','a.uid=c.id','LEFT')
                ->where($allwhere)->sum('a.money');
        }
        return $this->fetch('', ['title' => '用户资金明细','list'=>$list]);
    }
    /**
     * 商家
     */
    public function business(){
        list($get, $db) = [$this->request->get(), Db::name($this->capital_water)->alias('a')->join('t_capital b','a.cid=b.id','LEFT')->join('t_user c','a.uid=c.id','LEFT')->field('a.cid,a.type,a.addtime,a.order_id,a.money,a.name,c.username,c.nickname')];
        //时间
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $start = strtotime($start);
            $end = strtotime($end);
            $db->whereBetween('a.addtime', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        if(isset($get['type'])&&$get['type']!==''){
            $db->where("a.type",$get['type']);
        }
        //账号
        if(isset($get['username'])&&$get['username']!==''){
            $db->where("c.username",trim($get['username']));
        }
        //店铺id
        if(isset($get['shopid']) && $get['shopid'] !=''){
            $db->where("b.uid",$get['shopid']);
        }

        $where['b.type'] = 1;
        $list = parent::_list($db->where($where),1,'','');
        $capiMoodel = new capiModel();
        $list['list'] = $capiMoodel::getTypeName($list['list'],3);
        $list['list'] = parent::dateYmd($list['list']);
        $orderModel = new orderModel();
        $list['list'] = $orderModel->getSn($list['list']);

        //资金统计
        $list['allmoney'] = 0;
        $list['newmoney'] = 0;
        if(!empty($list['list'])){
            //当前页
            foreach ($list['list'] as $value){
                $list['newmoney']  += $value['money'];
            }
            //全部
            $allwhere =  'b.type = 1';
            //账号
            if(isset($get['username'])&&$get['username']!==''){
                $allwhere .= ' AND c.username='.trim($get['username']);
            }
            $list['allmoney'] = Db::name($this->capital_water)->alias('a')
                ->join('t_capital b','a.cid=b.id','LEFT')
                ->join('t_user c','a.uid=c.id','LEFT')
                ->where($allwhere)->sum('a.money');
        }
        return $this->fetch('', ['title' =>'商家资金明细','list'=>$list]);
    }

    /**
     * 所有
     */
    public function allCapital(){
        list($get, $db) = [$this->request->get(), Db::name($this->capital_water)->alias('a')->join('t_capital b','a.cid=b.id','LEFT')->join('t_user c','a.uid=c.id','LEFT')->field('a.cid,a.type,a.addtime,a.order_id,a.money,a.name,c.username,c.nickname')];
        //时间
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $start = strtotime($start);
            $end = strtotime($end);
            $db->whereBetween('a.addtime', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }

        if(isset($get['type'])&&$get['type']!==''){
            $db->where("a.type",$get['type']);
        }
        //账号
        if(isset($get['username'])&&$get['username']!==''){
            $db->where("c.username",$get['username']);
        }

        $list = parent::_list($db,1,'','');
        $capiMoodel = new capiModel();
        $list['type'] = $capiMoodel::cApiType(0);
        $list['list'] = $capiMoodel::getTypeName($list['list'],0);
        $list['list'] = parent::dateYmd($list['list']);
        $orderModel = new orderModel();
        $list['list'] = $orderModel->getSn($list['list']);
        return $this->fetch('', ['title' =>'所有资金管理','list'=>$list]);
    }

    public function exportExcle(){

    }

}